package com.huatai.model.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.huatai.common.api.R;
import com.huatai.model.entity.DictionaryEntity;
import com.huatai.model.service.DictionaryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import java.util.List;

@RestController
@RequestMapping("/dictionary")
@Api(value = "字典管理",tags = "字典管理")
public class DictionaryController extends BaseController<DictionaryService, DictionaryEntity>{

	@ApiOperation(value = "获取数据-返回树形列表",
		notes = "采用Mybatis方式",
		position = 2000)
	@PostMapping(value = "/list")
	public R list() {
		List<DictionaryEntity> dbDatas = this.service.list();
		return R.data(service.buildExInfo(dbDatas));
	}

	@ApiOperation(value = "删除数据",
		notes = "根据主键删除，不验证主键是否存在",
		position = 7000)
	@DeleteMapping("/delete/{id}")
	public R delete(@PathVariable("id") Long id) {
		LambdaQueryWrapper<DictionaryEntity> qw = Wrappers.lambdaQuery();
		qw.eq(DictionaryEntity::getPid,id);
		List<DictionaryEntity> list = service.list(qw);
		if(null != list && list.size()>0){
			return R.fail("不是叶子节点，不能删除");
		}
		this.service.removeById(id);
		return R.success("success");
	}
}
